/*
 * @Author: wuchenge
 * @Date:   2020-11-30 16:34:17
 * @Last Modified by:   wuchenge
 * @Last Modified time: 2020-11-30 16:41:38
 */

import axios from 'axios'
import { router, initRouter } from '../router/index'

axios.defaults.withCredentials = false
axios.defaults.baseURL = process.env.URL
axios.interceptors.request.use(
    config => {
        let token = window.localStorage.getItem('token')
        if (token) {
            config.headers.Authorization = token
        }
        return config
    },
    error => {
        return Promise.reject(error)
    }
)

axios.interceptors.response.use((response) => {

    if (!response.status && response.status != 200) {
        router.push({ name: 'home_index' })
    }
    const data = response.data

    if (data.error_code === 100401) {
        router.push({ name: 'home_index' })
    }
    if (data.error_code === 100409) {
        router.push({ name: 'home_index' })
    }
    if (data.error_code === 100500) {
        router.push({ name: 'error-403' })
    }
    return data
})

export default axios